package notification

import (
	"context"

	"yunzhan/alert-server/internal/svc"
	"yunzhan/alert-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteNotificationLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewDeleteNotificationLogic 删除通知
func NewDeleteNotificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteNotificationLogic {
	return &DeleteNotificationLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteNotificationLogic) DeleteNotification(req *types.DeleteNotificationRequest) (resp *types.DeleteNotificationResponse, err error) {
	var deletedNotifications []*types.Notification

	for _, id := range req.ID {
		// 查找通知记录
		notification, err := l.svcCtx.NotificationModel.FindOne(id)
		if err != nil {
			logx.Errorf("查找通知记录失败，ID: %d, 错误: %v", id, err)
			return &types.DeleteNotificationResponse{
				Success: 0,
				Message: "Failed to find notification",
				Error:   err.Error(),
			}, err
		}

		// 删除通知记录
		err = l.svcCtx.NotificationModel.Delete(id)
		if err != nil {
			logx.Errorf("删除通知记录失败，ID: %d, 错误: %v", id, err)
			return &types.DeleteNotificationResponse{
				Success: 0,
				Message: "Failed to delete notification",
				Error:   err.Error(),
			}, err
		}

		// 添加到已删除的通知列表
		deletedNotifications = append(deletedNotifications, notification)
	}

	// 返回删除成功的详细信息
	return &types.DeleteNotificationResponse{
		Success:              1,
		Message:              "Notification(s) deleted successfully",
		DeletedNotifications: deletedNotifications,
	}, nil
}
